﻿using System;
using System.Linq;
using System.Collections.Generic;
using Microsoft.WindowsAzure.StorageClient;
using System.ComponentModel.DataAnnotations;

namespace LCA.Community.DAL.Model
{
    /// <summary>
    /// This class represent a post of a blog.
    /// 
    /// RowKey = ID of the post
    /// PartitionKey = Owner's ID of the post
    /// </summary>
    public class Post : BasicDataModel
    {
        #region Fields
        #endregion

        /// <summary>
        /// Initializes a new instance of the <see cref="Post"/> class.
        /// </summary>
        public Post()
        {

        }

        /// <summary>
        /// Initializes a new instance of the <see cref="Post"/> class.
        /// </summary>
        /// <param name="owner">The owner.</param>
        /// <param name="author">The author.</param>
        /// <param name="title">The title.</param>
        /// <param name="body">The body.</param>
        /// <param name="tags">The tags.</param>
        public Post(string owner, string author, string title, string body, string tags)
        {
            RowKey = Guid.NewGuid().ToString();
            PartitionKey = owner;
            Date = DateTime.UtcNow;
            Title = title;
            Body = body;
            Tags = tags;
            Author = author;
        }

        /// <summary>
        /// Gets the short title of the post.
        /// </summary>
        /// <returns>The short title of the post.</returns>
        public String GetShortTitle()
        {
            if (Title.Length <= 58)
            {
                return Title;
            }
            else
            {
                return String.Format("{0}...", Title.Substring(0, 55));
            }
        }

        /// <summary>
        /// Equalses the specified obj.
        /// </summary>
        /// <param name="obj">The obj.</param>
        /// <returns><c>true</c> if both objects are equals; otherwise, <c>false</c>.</returns>
        public override bool Equals(object obj)
        {
            if (obj == null)
            {
                return false;
            }

            Post u = obj as Post;
            if ((System.Object)u == null)
            {
                return false;
            }

            return RowKey.Equals(u.RowKey);

        }
        /// <summary>
        /// Gets the hash code.
        /// </summary>
        /// <returns></returns>
        public override int GetHashCode()
        {
            return base.GetHashCode();
        }
        /// <summary>
        /// Gets the brief of the post
        /// </summary>
        /// <returns>The brief of the post</returns>
        public string Brief()
        {
            try
            {
                return System.Text.RegularExpressions.Regex.Replace(Body.Substring(0, Body.IndexOf('.', 200) + 1), "<[^>]*>", string.Empty);
            }
            catch
            {
                return System.Text.RegularExpressions.Regex.Replace(Body, "<[^>]*>", string.Empty);
            }
        }

        /// <summary>
        /// Gets or sets the owner.
        /// </summary>
        /// <value>The owner.</value>
        public string Owner
        {
            get
            {
                return PartitionKey;
            }
            set
            {
                PartitionKey = Owner;
            }
        }
        /// <summary>
        /// Gets or sets the title of the post.
        /// </summary>
        /// <value>The post's title.</value>
        [Required(ErrorMessage = "Debe introducir un título.")]
        public string Title { get; set; }
        /// <summary>
        /// Gets or sets the body of the post.
        /// </summary>
        /// <value>The post's body.</value>      
        public string Body { get; set; }
        /// <summary>
        /// Gets or sets the tags of the post.
        /// </summary>
        /// <value>The post's tags.</value>
        public string Tags { get; set; }
        /// <summary>
        /// Gets or sets the author's id of the post.
        /// </summary>
        /// <value>The author's id of the post.</value>
        public string Author { get; set; }
        /// <summary>
        /// Gets or sets the date of the post.
        /// </summary>
        /// <value>The post's date.</value>
        public DateTime Date { get; set; }
    }
}
